function [ varargout ] = showMRI( varargin )
%SHOWMRI Summary of this function goes here
%   Detailed explanation goes here
gui_Singleton = 0;
gui_State = struct('gui_Name', mfilename, ...
                    'gui_Singleton', gui_Singleton, ...
                    'gui_OpeningFcn', @showMRI_OpeningFcn, ...
                    'gui_OutputFcn', @showMRI_OutputFcn, ...
                    'gui_LayoutFcn', [],...
                    'gui_Callback', []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

end

function showMRI_OpeningFcn(hobj,event,handles,varargin)
set(hobj,'numbertitle','off');
if length(varargin)>=2
set(hobj,'name',['MRI Image Show: ',varargin{2}]);
end


%%% ----------------------------
%%% Here is for displaying the results of divasl
%%% Cannot be used for mono-TI
if length(varargin)>2
    handles.dDivasl = varargin{3};
    handles.flagRes = varargin{4};
else
    handles.dDivasl = [];
end
%%%%%%%-------------------------

handles.dMRI = varargin{1}; % get the handle of the dMRI

handles = initialize(hobj,handles);
guidata(hobj,handles);

end

function showMRI_OutputFcn(hobj,event,handles)


end


function handles = initialize(hobj,handles)

% --------------------------
% slider
tSlice = handles.dMRI.sImg;
set(handles.slider1,'Min',1,...
                    'SliderStep',[1/(tSlice-1),1/(tSlice-1)],...
                    'Value',1,...
                    'Max',tSlice);
set(handles.slider1,'Callback',@slideSlice);
handles.tSlice = tSlice;

%---------------------------
% edit1
cSlice = 1; % current slice
set(handles.edit1,'String',[num2str(cSlice),'/',num2str(tSlice)]);
set(handles.edit1,'Callback',@putSlice);
handles.cSlice = cSlice;

% --------------------------
% edit 2
% edit 3
set(handles.edit2,'String','0');
% set(handles.edit3,'String',num2str(max(max(handles.dMRI.getImg('mri',:,:,cSlice,1)))));
set(handles.edit3,'String','1000');


%--------------------------
% axes1
handles.CLOW = 0;
handles.CHIGH = max(max(handles.dMRI.getImg('mri',:,:,cSlice,1)));
if handles.CLOW == handles.CHIGH
    handles.CHIGH = handles.CLOW+1;
end
axes(handles.axes1);
imagesc(handles.dMRI.getImg('mri',:,:,cSlice,1),[handles.CLOW handles.CHIGH]);colormap gray; axis off; axis equal;axis image
if cSlice+1 <= tSlice
    axes(handles.axes2);
    imagesc(handles.dMRI.getImg('mri',:,:,cSlice+1,1),[handles.CLOW handles.CHIGH]);colormap gray; axis off; axis equal;axis image
end


hist(handles.axes3,handles.dMRI.getImg('mri',:,:,cSlice,1));axis('tight');
    



% -------------------------
% Window Button
set(hobj,'windowbuttonmotionfcn',@moveMousePoint);
set(hobj,'windowkeypressfcn',@slideSlice);
set(hobj,'windowbuttondownfcn',@selectVoxel);


end

function slideSlice(src,event)
if ~isempty(event)
    returnPress = 0;
    if ~strcmp(event.Key,'return')
        return;
    else
        returnPress = 1;
    end
end
handles = guidata(src);
    
oldCSlice = handles.cSlice;
handles.cSlice = round(get(handles.slider1,'Value'));
% If no different slice is selected
if oldCSlice~=handles.cSlice || returnPress
    set(handles.slider1,'Value',handles.cSlice);
    set(handles.edit1,'String',[num2str(handles.cSlice),'/',num2str(handles.tSlice)]);
    
    % display them

    cSlice = handles.cSlice;
    tSlice = handles.tSlice;
    
    handles.CLOW = str2double(get(handles.edit2,'String'));
    handles.CHIGH = str2double(get(handles.edit3,'String'));
    if handles.CLOW == handles.CHIGH
        handles.CHIGH = handles.CLOW+1;
    end
    axes(handles.axes1);
    imagesc(handles.dMRI.getImg('mri',:,:,cSlice,1),[handles.CLOW, handles.CHIGH]);colormap jet; axis off; axis equal;axis image
    if cSlice+1 <= tSlice
        axes(handles.axes2);
        imagesc(handles.dMRI.getImg('mri',:,:,cSlice+1,1),[handles.CLOW handles.CHIGH]);colormap jet; axis off; axis equal;axis image
    end
    
    temp = handles.dMRI.getImg('mri',:,:,cSlice,1);
    edges = [handles.CLOW:(handles.CHIGH-handles.CLOW)/100:handles.CHIGH];
    bar(handles.axes3,edges,histc(temp(abs(temp)>0),edges),'histc');axis('tight');
    set(handles.axes3,'XLim',[handles.CLOW,handles.CHIGH]);
    %     if cSlice-1 >=1
%         axes(handles.axes3);
%         imagesc(handles.dMRI.getImg('mri',:,:,cSlice-1,1));colormap hot; axis off; axis equal;axis image
%         
%     end
    
else
end
% disp(get(handles.slider1,'Value'));

guidata(src,handles);
end


function moveMousePoint(src,event)
handles = guidata(src);
position = get(handles.axes1,'CurrentPoint');

x = round(position(1));
y = round(position(3));
% axes(handles.axes1);
% title(['At point (',num2str(y),',',num2str(x),')']);
sizeImg = handles.dMRI.getImg('size');
if x<=sizeImg(2)&&x>0&&y<=sizeImg(1)&&y>0
    handles.x = x;
    handles.y = y;

% disp(handles.y);
Infos = ['(',num2str(handles.x),',',num2str(handles.y),')',...
    '-->',num2str(handles.dMRI.getImg('mri',handles.y,handles.x,handles.cSlice,1))];
% if src==1
%     disp(Infos);
% end
set(handles.text1,'String',Infos);
end
end


function selectVoxel(src,event)
handles = guidata(src);
position = get(handles.axes1,'CurrentPoint');

x = round(position(1));
y = round(position(3));
% axes(handles.axes1);
% title(['At point (',num2str(y),',',num2str(x),')']);
sizeImg = handles.dMRI.getImg('size');
if x<=sizeImg(1)&&x>0&&y<=sizeImg(2)&&y>0
    handles.x = x;
    handles.y = y;

% disp(handles.y);
Infos = ['(',num2str(handles.x),',',num2str(handles.y),')',...
    '-->',num2str(handles.dMRI.getImg('mri',handles.y,handles.x,handles.cSlice,1))];
% if src==1
    disp(Infos);
% end
% set(handles.text1,'String',Infos);


%%%%%%----------------------------
%%%%%%% The code here is just for divasl
%%%%%%%
if ~isempty(handles.dDivasl)
    if ~isfield(handles,'fCurves')
        handles.fCurves = figure('name','Curves','numbertitle','off');
    else
        figure(handles.fCurves);
    end
    
    obj_divasl = handles.dDivasl;
    switch lower(handles.flagRes)
        case 'ls'
            results = obj_divasl.dLS;
%             cbf = handles.dDivasl.dLS.dCBF.getImg('mri',handles.y,handles.x,handles.cSlice,1);
%             deltat = handles.dDivasl.dLS.dDT.getImg('mri',handles.y,handles.x,handles.cSlice,1);
        case 'cmf'
            results = obj_divasl.dCMF;
%             cbf = handles.dDivasl.dCMF.dCBF.getImg('mri',handles.y,handles.x,handles.cSlice,1);
%             deltat = handles.dDivasl.dCMF.dDT.getImg('mri',handles.y,handles.x,handles.cSlice,1);
    end
    cbf = results.dCBF.getImg('mri',handles.y,handles.x,handles.cSlice,1);
    deltat = results.dDT.getImg('mri',handles.y,handles.x,handles.cSlice,1);
    dataDT = obj_divasl.getImg('divti',handles.y,handles.x,handles.cSlice,1,results.maskType);
    
    TIS = obj_divasl.TIs+(handles.cSlice-1)*0.045;
    plot(TIS, dataDT, 'x-' ); 
    xlabel('Sampling time (TI)');ylabel('\Delta M');
    
    
    M0 = obj_divasl.dM0.dMRI(handles.y,handles.x,handles.cSlice,1);
    obj_divasl.dModel.pM0 = M0;
    
    x(1) = cbf;x(2) = deltat;
    
    t = 0:0.001:3;
    [ estCurves,t ] = handles.dDivasl.dModel.curvefitting_asl_model(x,t);
    title(['M0=',num2str(M0),'; CBF=',num2str(cbf),'; AAT=',num2str(deltat)]);
    hold on;
    plot(t, estCurves,'r-','LineWidth',2);
    axis tight;
    xlim([0,3]);
    
%     for i = 1:length(handles.dDivasl.TIs)
%         dAsl = handles.dDivasl.dDivasl(i).getASL('mri',handles.y,handles.x,handles.cSlice,results.numRep);
%         plot(TIS(i),dAsl(:),'xg');
%         axis tight;
%     end
    
    hold off
    
end
guidata(src,handles);

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end
